<?php 
namespace Partner\Controller;

use Utuo\BackgroundController;
use Utuo\PHPExcel;

class PartnerLoginLogController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle('登录日志');
		$this->setActionTitle(array(
				'index' => '登录日志',
		));
		$this->setActionNav(array(
				'index',
		));
	}
	
	public function getLists( $is_deleted = 0 ) {
		$pageSize = $this->pageSize;
		$page = $this->getPage();
		$startDate = I('startDate', 0, 'intval') or $startDate = strtotime('-29 days');
		$endDate = I('endDate', 0, 'intval') or $endDate = time();
		$params = array(
				'startDate' => $startDate,
				'endDate' => $endDate,
		);
		$name = I('name', '', "trim") and $params['name'] = $name;
		$partnerLoginLogList = $this->search($params, true);
		$this->assign("params", $params);
		$this->assign("partnerLoginLogList", $partnerLoginLogList);
		$this->display();
	}
	
	/**
	 * 导出Excel
	 */
	public function exportXLSAction() {
		$startDate = I('startDate', 0, 'intval') or $startDate = strtotime('-29 days');
		$endDate = I('endDate', 0, 'intval') or $endDate = time();
		$params = array(
				'startDate' => $startDate,
				'endDate' => $endDate,
		);
		$lists = $this->search( $params, false );
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setCreator("微美风铃")
					->setLastModifiedBy("微美风铃")
					->setTitle("登录日志记录")
					->setSubject("登录日志记录")
					->setDescription("登录日志记录")
					->setKeywords("登录日志")
					->setCategory("登录日志");
	
	
		/*设置标题*/
		$titles = array('ID','管理员','IP','登入时间','最后活跃时间','登出时间');
		$fields = array('id' => array('type'=>'number'),
				'adminName'=> array('type'=>'string'),
				'ip'=> array('type'=>'string'),
				'create_time'=>array('type'=>'datetime', 'format' => 'Y/m/d h:i:s'),
				'last_activity_time'=>array('type'=>'datetime', 'format' => 'Y/m/d h:i:s'),
				'loginout_time'=>array('type'=>'datetime', 'format' => 'Y/m/d h:i:s'));
		$sheet0 = $objPHPExcel->setActiveSheetIndex(0);
		$rowNumber = 1;
		foreach ($titles as $k => $v) {
			$sheet0->setCellValue(chr(ord('A')+$k).$rowNumber, $v);
		}
		$rowNumber++;
		foreach ( $lists as $v ) {
			$fieldIndex = 0;
			foreach ( $fields as $k0 => $v0 ) {
				$cellName = chr(ord('A')+$fieldIndex++).$rowNumber;
				$sheet0->setCellValueExplicit($cellName, $this->getShowData($v[$k0], $v0), $this->getCellFormat($v[$k0]));
				//$sheet0->setCellValue($cellName, );
			}
			$rowNumber++;
		}
		$objPHPExcel->getActiveSheet()->setTitle(date('Y年m月d', time($startDate)).'至'.date('Y年m月d', time($endDate))."登录日志");
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.date("Ymdhis").'.xls"');
		header('Cache-Control: max-age=0');
		header('Cache-Control: max-age=1'); // IE9时
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
	}
	
	/**
	 * 获得单元格格式
	 * @param unknown $fieldCfg
	 * @return string
	 */
	private function getCellFormat( $fieldCfg ) { 
		if( $fieldCfg['type'] == 'string' ) {
			return \PHPExcel_Cell_DataType::TYPE_STRING;
		}elseif( $fieldCfg['type'] == 'money' ) {
			return \PHPExcel_Cell_DataType::TYPE_NUMERIC;
		}
		return \PHPExcel_Cell_DataType::TYPE_STRING;
	}
	
	/**
	 * 获得数据展示格式
	 * @param unknown $value
	 * @param unknown $fieldCfg
	 * @return string|unknown
	 */
	private function getShowData($value, $fieldCfg) {
		if( $fieldCfg['type'] == 'datetime' ) {
			return date( $fieldCfg['format'], $value);
		}
		return $value;
	}
	
	/**
	 * 查询
	 * @param unknown $params
	 * @return unknown
	 */
	private function search( $params, $paginate = true ) {
		$page = $this->getPage();
		$condition = array(
				'a.pid' => $this->aid,
				'a.create_time' => array(array('egt', $params['startDate']), array('elt', $params['endDate']), 'and'),
		);
		$params['name'] and $condition['name'] = array('like', "%{$params['name']}%");
		$m = M("PartnerLoginLog a")->join("__PARTNER_ADMIN__ b on a.admin_id=b.id")->where($condition);
		$n = clone $m;
		if( $paginate ) {
			$totalRows = $n->count();
			$m->page($page)->limit($this->getPageSize());
			$this->showPage($totalRows, $this->pageSize);
		}
		$lists = $m->field("a.*,b.name as adminName")->select();
		return $lists;
	}
	
}